<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="考试院系">
              <a-select :disabled="selectMenu.yxList.length === 1" allowClear v-model="queryParam.deptCode" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="教室类型">
              <a-select allowClear v-model="queryParam.typeCode">
                <a-select-option v-for="i in TYPE0046" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="教室名称">
                <a-input allowClear v-model="queryParam.kcjsbh"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="考试课程">
                <a-select allowClear v-model="queryParam.kcbm" showSearch :filterOption="filterOption">
                  <a-spin v-if="kcLoading" slot="notFoundContent" size="small"/>
                  <a-select-option v-for="(i,index) in selectMenu.courseList" :key="index" :value="i.kch">{{ i.kch }}-{{ i.kcmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="考试时间">
                <a-date-picker v-model="queryParam.ksrq" allowClear/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="16" :sm="24">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="toAdd" style="margin:0 10px">新增考试安排</a-button>
            <a-button type="primary" @click="batchDelete" style="margin:0 10px">删除</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{ x: 1400 }"
      >
        <span slot="kcmc" slot-scope="kcmc,record">{{ record.kcbm }}-{{ record.kcmc }}</span>
        <template slot="action" slot-scope="id,record">
          <a @click="showEdit(record.id)">修改</a>
          <a-divider type="vertical" />
          <a-popconfirm title="是否确定删除该考试安排" @confirm="deleteExam(record.id)" okText="确定" cancelText="取消">
            <a>删除</a>
          </a-popconfirm>
        </template>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import searchMixin from '@/utils/searchRemain'

export default {
  mixins: [ searchMixin ],
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '所属院系',
          dataIndex: 'ksyxmc',
          width:180
        },
        {
          title: '教室名称',
          width: '120px',
          dataIndex: 'kcjsbh'
        },
        {
          title: '教室类型',
          width: '120px',
          dataIndex: 'typeCodeName'
        },
        {
          title: '教室位置',
          width: '120px',
          dataIndex: 'location'
        },
        {
          title: '考试课程',
          dataIndex: 'kcmc',
          scopedSlots: { customRender: 'kcmc' },
        },
        {
          title: '开始课节',
          width: '100px',
          dataIndex: 'kskj'
        },
        {
          title: '结束课节',
          width: '100px',
          dataIndex: 'jskj'
        },
        {
          title: '开始时间',
          width: '180px',
          dataIndex: 'kskssj'
        },
        {
          title: '结束时间',
          width: '180px',
          dataIndex: 'ksjssj'
        },
        {
          title: '操作',
          width: '120px',
          scopedSlots: { customRender: 'action' },
          fixed: 'right',
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign({}, this.queryParam, parameter)
        params.ksrq = params.ksrq ? params.ksrq.format('YYYY-MM-DD') : ''
        return this.$api.cultivate.examArrangeList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yxList:[]
      },
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      kcLoading: false,
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0046: state => state.app['TYPE0046']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0046')
    // this.initSelectMenu()
    this.$store.dispatch('app/sysInfo', true).then(data => {
      if ( +new Date() > +data.ksqjjssj  ||  +new Date() < +data.ksqjkssj) {
        this.$confirm({
          title: '考试时间已过，请联系管理员',
          centered: true,
          okText: '确认',
          cancelText: '返回',
          onOk: () => {
            this.$router.back()
          },
          onCancel: () => {
            this.$router.back()
          }
        })
      }
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'deptCode', this.selectMenu.yxList[0].dwh)
          this.yxChange(this.queryParam.deptCode)
        }
        this.initDone = true
        // this.$nextTick(() => {
        //   this.$refs.table.refresh(true)
        // })
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    yxChange (code) {
      this.$set(this.queryParam, 'kcbm', '')
      this.$set(this.selectMenu, 'courseList', [])
      if (code) {
        this.kcLoading = true
        this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
          this.$set(this.selectMenu, 'courseList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'courseList', [])
      }
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的考试安排')
        return
      }
      this.$confirm({
        title: '确定删除所选中的考试安排?',
        content: '当您点击确定按钮后，这些记录将会被删除',
        centered: true,
        onOk: () => {
          let ids = this.selectedRowKeys.join(',')
          this.$api.cultivate.examDelete(ids).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    deleteExam(id) {
      this.$api.cultivate.examDelete(id).then(() => {
        this.$message.success('删除成功')
        this.selectedRowKeys = []
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
      })
    },
    showEdit (id) {
      this.$router.push(`/cultivate/exam/add?id=${id}`)
    },
    toAdd () {
      this.$router.push('/cultivate/exam/add')
    }
  }
}
</script>

<style>

</style>
